package com.park.user.service.licenseplate.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.park.comm.ResultEnum;
import com.park.dao.mapper.user.UserLicensePlateMapper;
import com.park.dao.mapper.user.UserMapper;
import com.park.entity.licenseplate.LicensePlateDto;
import com.park.entity.user.UserInfo;
import com.park.entity.user.UserLicensePlate;
import com.park.exception.CommException;
import com.park.user.service.licenseplate.UserLicensePlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Administrator
 * @title: UserLicensePlateServiceImpl
 * @projectName park
 * @description: 用户车牌业务类
 * @date 2020/5/212:28
 */
@Service(value = "userLicensePlateService")
public class UserLicensePlateServiceImpl implements UserLicensePlateService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserLicensePlateMapper userLicensePlateMapper;

    private static Lock lock = new ReentrantLock();


    /**
     　　* @description: 用户绑定车牌号
     　　* @param [licensePlateDto]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/2 12:59
     　　*/
    public void addLicenseplate(LicensePlateDto licensePlateDto) {
        lock.lock();
        try {
            Integer userid = licensePlateDto.getUserid();
            UserLicensePlate userLicensePlateDate = userLicensePlateMapper.selectByUserId(userid);
            if(ObjectUtil.isNotEmpty(userLicensePlateDate)){
                throw new CommException(ResultEnum.USER_EXIST_ERROR);
            }
            String licenseNumber = licensePlateDto.getLicensePlate();
            //组装数据
            UserLicensePlate userLicensePlate = new UserLicensePlate();
            if(StrUtil.isNotBlank(licenseNumber) && licenseNumber.length()>=5){
                UserInfo userInfo = userMapper.selectByPrimaryKey(userid);
                String openId = userInfo.getOpenId();
                long timeStamp = System.currentTimeMillis();
                String licensePlateArea = licenseNumber.substring(0, 2);
                String licensePlate = licenseNumber;
                //插入数据库
                userLicensePlate.setUserId(userid);
                userLicensePlate.setAddTimeStamp(timeStamp);
                userLicensePlate.setOpenId(openId);
                userLicensePlate.setTimeStamp(timeStamp);
                userLicensePlate.setLicensePlateArea(licensePlateArea);
                userLicensePlate.setLicensePlate(licensePlate);
                userLicensePlateMapper.insert(userLicensePlate);
            }else{
                throw new CommException(ResultEnum.CAR_PLATE_ERROR);
            }
        }finally {
            //程序解锁
            lock.unlock();
        }
    }

    /**
     　　* @description: 用户修改车牌号
     　　* @param [licensePlateDto]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/2 12:59
     　　*/
    public void editLicenseplate(LicensePlateDto licensePlateDto) {
        Integer userid = licensePlateDto.getUserid();
        UserLicensePlate userLicensePlate = userLicensePlateMapper.selectByUserId(userid);
        String licenseNumber = licensePlateDto.getLicensePlate();
        if(StrUtil.isNotBlank(licenseNumber)){
            String licensePlateArea = licenseNumber.substring(0, 2);
            String licensePlate = licenseNumber;
            userLicensePlate.setLicensePlateArea(licensePlateArea);
            userLicensePlate.setLicensePlate(licensePlate);
            userLicensePlate.setTimeStamp(System.currentTimeMillis());
            userLicensePlateMapper.updateByPrimaryKeySelective(userLicensePlate);
        }
    }


    /**
     　　* @description:
     　　* @param [userId 用户id]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/6 20:11
     　　*/
    public void delLicenseplate(Integer userId) {
        UserLicensePlate userLicensePlate = userLicensePlateMapper.selectByUserId(userId);
        userLicensePlateMapper.deleteByPrimaryKey(userLicensePlate.getId());
    }

       /**
       　　* @description:  查询用户车牌
       　　* @param userId
       　　* @return {@link null}
       　　* @throws
       　　* @author ZhangHaiWen
       　　* @version 2.0
       　　* @date 2020/5/6 21:06
       　　*/
    public LicensePlateDto queryLicenseplate(Integer userId) {
        UserLicensePlate userLicensePlate = userLicensePlateMapper.selectByUserId(userId);
        LicensePlateDto licensePlateDto = new LicensePlateDto();
        if(ObjectUtil.isNotEmpty(userLicensePlate )){
            licensePlateDto.setLicensePlate(userLicensePlate.getLicensePlate());
        }
        return licensePlateDto;
    }

}
